#include <QObject>
#include <QFileDialog>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QDebug>
#include <QDateEdit>

#include "passportwidget.h"
#include "mainwindow.h"
#include "ui_passportwidget.h"
#include "manager.h"
#include "defs.h"

PassportWidget::PassportWidget(QWidget *parent) :
    QWidget (parent),
    ui (new Ui::PassportWidget),
    mFotoItem (NULL)
{
    ui->setupUi(this);
    mFotoPath = "";
    ui->gwFoto->setScene(&mFotoScene);

    mHeightValidator = new QIntValidator(20, 400, this);

    ui->edtFathersHeight->setValidator(mHeightValidator);
    ui->edtMothersHeight->setValidator(mHeightValidator);

    ui->dateEdtDob->setDate(QDate::currentDate());

    ui->cbBloodType->insertItems(0, BloodTypes);
    ui->cbBloodType->setCurrentIndex(0);
    ui->cbBloodRh->insertItems(0, BloodRh);
    ui->cbBloodRh->setCurrentIndex(0);
    ui->cbRegion->insertItems(0, RegionStrings);
    ui->cbRegion->setCurrentIndex(0);
}

PassportWidget::~PassportWidget()
{
    delete ui;
}

void PassportWidget::setup()
{
    ui->edtName->setText( App->pacient()->fName() );
    ui->edtSurname->setText( App->pacient()->lName() );
    ui->edtSecName->setText( App->pacient()->mName() );

    loadFoto( App->pacient()->foto() );

    ui->dateEdtDob->setDate(App->pacient()->dob());

    ui->cbSex->setCurrentIndex(
                App->pacient()->sex()=="чол" ? 0 : 1);
    double fh = App->pacient()->hFather();
    ui->edtFathersHeight->setText(
                fh>0 ? QString::number(fh) : QString(""));
    double mh = App->pacient()->hMother();
    ui->edtMothersHeight->setText(
                mh>0 ? QString::number(mh) : QString(""));
    ui->teComment->setText(App->pacient()->comment());

    ui->cbBloodType->setCurrentIndex(App->pacient()->mBlood.first);
    ui->cbBloodRh->setCurrentIndex(App->pacient()->mBlood.second);

    ui->cbRegion->setCurrentIndex(App->pacient()->mRegion);
    ui->edtTown->setText( App->pacient()->mTown );

    ui->cbSmoke->setChecked( App->pacient()->mSmoke );
    ui->cbAlko->setChecked( App->pacient()->mAlko );
    ui->cbNarco->setChecked( App->pacient()->mNarco );

    // connecting all to Pacient object
    connect(ui->edtName, SIGNAL(textChanged(QString)),
            App->pacient(), SLOT(setFName(QString)));
    connect(ui->edtSecName, SIGNAL(textChanged(QString)),
            App->pacient(), SLOT(setMName(QString)));
    connect(ui->edtSurname, SIGNAL(textChanged(QString)),
            App->pacient(), SLOT(setLName(QString)));

    connect(ui->edtFathersHeight, SIGNAL(textChanged(QString)),
            App->pacient(), SLOT(setHFather(QString)));
    connect(ui->edtMothersHeight, SIGNAL(textChanged(QString)),
            App->pacient(), SLOT(setHMother(QString)));

    connect(ui->dateEdtDob, SIGNAL(dateChanged(QDate)),
            App->pacient(), SLOT(setDob(QDate)));

    connect(ui->cbSex, SIGNAL(currentIndexChanged(QString)),
            App->pacient(), SLOT(setSex(QString)));
    connect(ui->teComment, SIGNAL(textChanged()),
            this, SLOT(setCommentToPacient()));

    connect(ui->cbBloodType, SIGNAL(currentIndexChanged(int)),
            App->pacient(), SLOT(setBloodType(int)));
    connect(ui->cbBloodRh, SIGNAL(currentIndexChanged(int)),
            App->pacient(), SLOT(setBloodRh(int)));

    connect(ui->cbRegion, SIGNAL(currentIndexChanged(int)),
            App->pacient(), SLOT(setRegion(int)));
    connect(ui->edtTown, SIGNAL(textChanged(QString)),
            App->pacient(), SLOT(setTown(QString)));

    connect(ui->cbSmoke, SIGNAL(toggled(bool)),
            App->pacient(), SLOT(setSmoke(bool)));
    connect(ui->cbAlko, SIGNAL(toggled(bool)),
            App->pacient(), SLOT(setAlko(bool)));
    connect(ui->cbNarco, SIGNAL(toggled(bool)),
            App->pacient(), SLOT(setNarco(bool)));

}


void PassportWidget::loadFoto(const QString &path)
{
    if (mFotoItem!=NULL)
    {
        mFotoScene.removeItem(mFotoItem);
        mFotoItem = NULL;
    }
    qDebug() << "LoadFoto: " << path;

    QString fileName ( App->copyImage( QDir::toNativeSeparators(path) ) );

    if (fileName.isNull())
    {
        LOG ("loadFoto", "Не можу завантажити фото", fileName);
        return;
    }
    QDir tmpDir(App->curTempDir());
    QImage image(tmpDir.filePath(fileName) );

    mFotoItem = new QGraphicsPixmapItem(QPixmap::fromImage(image)
                                        .scaled(QSize(ui->gwFoto->size()),
                                                Qt::KeepAspectRatio ));
    mFotoScene.addItem(mFotoItem);

    mFotoPath = fileName;
    App->pacient()->setFoto( fileName );
}

void PassportWidget::clearAll()
{
    // clearing all QLineEdits
    foreach(QObject *w, this->children())
    {
        QLineEdit *edt = qobject_cast<QLineEdit*>(w);
        if (edt!=NULL)
        {
            edt->setText("");
        }
    }
    ui->cbSex->setCurrentIndex(0);
    ui->dateEdtDob->setDate(QDate::currentDate());
    ui->teComment->clear();

    ui->cbBloodRh->setCurrentIndex(0);
    ui->cbBloodType->setCurrentIndex(0);

    if (mFotoItem!=NULL)
    {
        delete mFotoItem;
        mFotoItem = 0;
    }

    mFotoPath.clear();
    App->pacient()->setFoto("");
}

QString PassportWidget::name() const
{
    return ui->edtName->text();
}

QString PassportWidget::surname() const
{
    return ui->edtSurname->text();
}

QString PassportWidget::secName() const
{
    return ui->edtSecName->text();
}

QDate PassportWidget::dob() const
{
    return ui->dateEdtDob->date();
}

bool PassportWidget::isMan() const
{
    return ui->cbSex->currentIndex() == 0 ? true : false;
}

float PassportWidget::fathersHeight() const
{
    bool ok = false;
    float val = ui->edtFathersHeight->text().toDouble(&ok);
    return ok ? val : -1.0;
}

float PassportWidget::mothersHeight() const
{
    bool ok = false;
    float val = ui->edtMothersHeight->text().toDouble(&ok);
    return ok ? val : -1.0;
}

QString PassportWidget::fotoPath() const
{
    return mFotoPath;
}

QString PassportWidget::comment() const
{
    return ui->teComment->toPlainText();
}

void PassportWidget::createConnectToPacient()
{
    Q_ASSERT(App->pacient()!=NULL);
    connect (ui->dateEdtDob, SIGNAL(dateChanged(QDate)),
             App->pacient(), SLOT(setDob(QDate)));
}


void PassportWidget::on_btnChooseFoto_clicked()
{
    mFotoPath =
            QFileDialog::getOpenFileName(this,
                                         "Відкрити фото пацієнта",
                                         "",
                                         "Зображення (*.png *.jpg *.bmp)");
    loadFoto(mFotoPath);
}

void PassportWidget::setCommentToPacient()
{
    App->pacient()->setComment( ui->teComment->toPlainText() );
}
